//
//  PersonLoginVC.m
//  EJLX
//
//  Created by adc on 15/11/20.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PersonLoginVC.h"
#import "PersonVC.h"
#import "PublicNoticeVC.h"
#import "ShouYinDingDanViewController.h"
#import "ErWeiMaViewController.h"
#import "ShouCangJiaViewController.h"
@interface PersonLoginVC ()

@end

@implementation PersonLoginVC

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中..." animated:YES];
    [HttpsManager requestJianJieXinXiCompletion:^(XinXiJianJie *xinXi) {
       
        self.xinXi = xinXi;
        self.name = xinXi.name;
        UIView *headVC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDE, 180)];
        headVC.backgroundColor = RGB_COLOR(0x00c49f);
        
        _peopleImage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDE/2-40, 20, 80, 80)];
        _peopleImage.layer.cornerRadius = 40;
        _peopleImage.layer.masksToBounds = YES;
      
        [_peopleImage sd_setImageWithURL:[NSURL URLWithString:self.xinXi.avatar] placeholderImage:[UIImage imageNamed:@"头像.jpg"]];
         // _peopleImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.xinXi.avatar]]];
        _peopleImage.userInteractionEnabled = YES;
        _peopleImage.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [_peopleImage addGestureRecognizer:tap];
        [headVC addSubview:_peopleImage];
        
        UIImageView *xiangji = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"相机"]];
        xiangji.frame = CGRectMake(WIDE/2+5, 80, 30, 30);
        
        [headVC addSubview:xiangji];
        
        
        
        _userNameLB = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-80, 120, 160, 20 )];
        _userNameLB.text = self.xinXi.name;
        _userNameLB.textAlignment = NSTextAlignmentCenter;
        _userNameLB.textColor = [UIColor whiteColor];
        [headVC addSubview:_userNameLB];
        
        UILabel *userLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 140, WIDE, 20 )];
        
        userLB.text = [NSString stringWithFormat:@"级别:%@｜审核状态:%@",self.xinXi.levle,self.xinXi.status];
        userLB.font = [UIFont systemFontOfSize:12.0f];
        NSLog(@"nsme %@ %@ %@ %@",self.xinXi.name,self.xinXi.levle,self.xinXi.status,self.xinXi.avatar);
        userLB.textColor = [UIColor whiteColor];
        userLB.textAlignment = NSTextAlignmentCenter;
        [headVC addSubview:userLB];
        _tableView.tableHeaderView = headVC;
        
        
        
        
         [GMDCircleLoader hideFromView:self.view animated:YES];
        [_tableView reloadData];
    }];
}

-(void)backBtn{
    [self.navigationController popViewControllerAnimated:YES];

}
-(void)moreBtn{
    PersonVC *vc = [[PersonVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];

}
-(void)creatNav{
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(WIDE/2-60, 7, 120, 30  )];
    title.text = @"个人中心";
    title.textAlignment = NSTextAlignmentCenter;
    title.textColor = [UIColor whiteColor];
    [navView addSubview:title];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDE-50, 7, 30, 30);
    rightBtn.layer.cornerRadius = 20;
    [rightBtn setImage:[UIImage imageNamed:@"更多"] forState:UIControlStateNormal] ;
    [rightBtn addTarget:self action:@selector(moreBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightBtn];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
  // self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNav];

    self.titleArray =@[@"系统公告",@"我的订单",@"我要升级",@"操作手册",@"我的二维码",@"收藏夹"];
    self.imgArr = @[@"系统公告新",@"我的订单",@"我要升级",@"操作手册",@"我的二维码新",@"收藏夹"];
    
   
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDE, 500)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView =[[UIView alloc] init];
    [_tableView setScrollEnabled:NO];
    
    

    [self.view addSubview:_tableView];
    
   

//    UITapGestureRecognizer *TAP = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeImageForPeople)];
//    [peopleImage addGestureRecognizer:TAP];
    
    

    
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.titleArray[indexPath.row];
    cell.textLabel.textColor = [UIColor blackColor];
    
    cell.imageView.image = [UIImage imageNamed:self.imgArr[indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row ==0) {
        //系统公告
        PublicNoticeVC *public = [[PublicNoticeVC alloc] init];
        [self.navigationController pushViewController:public animated:YES];
    }else if (indexPath.row == 1){
        //我的订单
        ShouYinDingDanViewController *dingdan = [[ShouYinDingDanViewController alloc]  init];
        [self.navigationController pushViewController:dingdan animated:YES];
        
        
    }else if (indexPath.row == 2){
        //我要升级
        NSString *str = [NSString stringWithFormat:@"你当前级别是%@，客服电话：400-8729799\n           是否联系客服进行升级？",self.xinXi.levle];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str  delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        [alert show];
        
        

        
//        [HttpsManager requsetGetVersionForAPPBlock:^(NSDictionary *dict) {
//            NSString *version = [[dict objectForKey:@"data"] objectForKey:@"v_name"];
//            
//            NSString *currentVersion = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
//            
//            NSLog(@"current %@ \n version %@",currentVersion,version);
//            if (version == NULL) {
//                SHOW_ALERT(@"当前已是最新版本")
//            }
//            if (version>currentVersion) {
//                NSString *str = [NSString stringWithFormat:@"发现新版本(%@),是否更新?",version];
//                SHOW_ALERT(str)
//                //点击更新
//                
//            }
//        }];
        
    }
    else if (indexPath.row == 3){
        //操作手册
        GoToWebView *web = [[GoToWebView alloc] initWithurl:[NSURL URLWithString:[NSString stringWithFormat:@"%@comm/user_agreement",HTTPS_ID]] title:@"操作手册"];
        [self.navigationController pushViewController:web animated:YES];
    }else if (indexPath.row == 4){
        //我的二维码
        ErWeiMaViewController *wei = [[ErWeiMaViewController alloc] init];
        [self.navigationController pushViewController:wei  animated:YES];
        
    }else if (indexPath.row == 5){
        
        //收藏夹
        ShouCangJiaViewController *vc = [[ShouCangJiaViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    if (buttonIndex == 1) {
        UIWebView *web = [[UIWebView alloc] init];
        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel://400-8729799"]]];
        [self.view addSubview:web];

    }
}

-(void)tap{
    [self changeImageForPeople];
}
-(void)changeImageForPeople{
    //转改头像
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [sheet addAction:[UIAlertAction actionWithTitle:@"📷" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePicker = [[UIImagePickerController alloc] init];
        
        _imagePicker.delegate = self;
        
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        _imagePicker.allowsEditing = YES;
        
        [self presentViewController:_imagePicker animated:YES completion:nil];
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _imagePicker = [[UIImagePickerController alloc] init];
        
        _imagePicker.delegate = self;
        
        _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        _imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        _imagePicker.allowsEditing = YES;
        
        [self presentViewController:_imagePicker animated:YES completion:nil];
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:sheet completion:nil];
    }]];
    [self presentViewController:sheet animated:YES completion:nil];
    
}

#pragma mark - picker
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
   
      [GMDCircleLoader setOnView:self.view withTitle:@"头像上传中.........." animated:YES];
    
      UIImage * image = info[UIImagePickerControllerOriginalImage];
 
    
    [HttpsManager postimage:image block:^(NSString *err) {
      
       
        if ([err isEqualToString:@"ok"]) {
            [GMDCircleLoader hideFromView:self.view animated:YES];
            [_tableView reloadData];
            [self dismissViewControllerAnimated:YES completion:nil];

        }else{
             SHOW_ALERT(@"上传失败，请重新上传！");
        }
        
        
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
